﻿/**
 * 游戏的主要方法：
 * */
(
    function(){
        window.Game = Class.extend({
            //初始化方法
            init:function (option) {
                this.option = option ||{};
                //备份指针
                var self = this;
                //帧数
                this.fps = option.fps || 60;
                //获取画布和上下文
                this.canvas = document.getElementById(option.canvasId);
                this.ctx = this.canvas.getContext("2d");
                //实例化帧工具FrameUtil
                this.frameUtil = new FrameUtil();
                //实例化获取json数据的工具
                this.StaictSouceUtil = new StaictSouceUtil();
                this.StaictSouceUtil.onLoadImage('r.json',function (allImageObj,allImage,sLoadImageCount) {
                    //当加载的图片的数据等于所有图片的时候运行游戏
                   if(sLoadImageCount == allImage){
                       self.allImageObj =allImageObj;
                       self.run();
                   }
                    // console.log(allImageObj['fangzi']);
                })
            },

            //运行游戏
            run:function(){
                //备份指针
                var self = this;
                setInterval(function () {
                    self.runLook();
                },1000/self.fps);// 1s/总帧数  每帧的时间

                //实例化背景
                //绘制房子
                this.fangzi = new BackGround({
                    bgImg : this.allImageObj["fangzi"],
                    y:this.canvas.height-256-100,
                    width:300,
                    height:256,
                    speed:1
                });
                //绘制shu
                this.shu = new BackGround({
                    bgImg : this.allImageObj["shu"],
                    y:this.canvas.height-215-48,
                    width:300,
                    height:216,
                    speed:2
                });
                //绘制地板
                this.diban = new BackGround({
                    bgImg : this.allImageObj["diban"],
                    y:this.canvas.height-48,
                    width:48,
                    height:48,
                    speed:3
                })

            },

            //循环游戏
            runLook : function(){

                //调用FrameUtil中的render方法获取reaFps，currentFrame
                this.frameUtil.render();

                //清屏
                this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);
                this.ctx.fillText("真实的fps:"+this.frameUtil.reaFps,12,18);
                this.ctx.fillText("总帧数:"+this.frameUtil.currentFrame,12,36)

                //更新和描绘房子
                this.fangzi.update();
                this.fangzi.render();
                //更新和描绘树
                this.shu.update();
                this.shu.render();
                //更新和描绘地板
                this.diban.update();
                this.diban.render();

            },

            //暂停游戏
            pause : function(){

            },

            //停止游戏
            stop : function(){

            },
        })
    }
)();